import { reactive, ref } from 'vue'
import { TASK_TYPE, TASK_INSPECTS, LIVE_STREAM } from '@/utils/dictionary'
import { getIdDeptIdRoad } from '@/api/org-manage/depart-manage/index'
import { getAirList, getWayList, getAirLineByAir } from '@/api/task/index'
// import { getChannelListByUser } from '@/api/road/road-manage/index.js'
function disableTime(ts) {
  /* 当前日期 */
  const DATE = new Date().getDate()
  /* 当前小时 */
  const HOURS = new Date().getHours()
  /* 当前分钟 */
  const MINUTES = new Date().getMinutes()
  /* 当前秒 */
  const SECONDS = new Date().getSeconds()
  /* 选择日期 */
  const tDate = new Date(ts).getDate()
  /* 选择小时 */
  const tHours = new Date(ts).getHours()
  /* 选择分钟 */
  const tMinutes = new Date(ts).getMinutes()
  return {
    isHourDisabled: (hour) => {
      return tDate === DATE && hour < HOURS
    },
    isMinuteDisabled: (minute) => {
      if (tDate === DATE) {
        if (tHours > HOURS) {
          return false
        } else {
          return minute < MINUTES
        }
      } else {
        return false
      }
    },
    isSecondDisabled: (second) => {
      /* 日期相同 */
      if (tDate === DATE) {
        if (tHours > HOURS) {
          return false
        } else if (tMinutes > MINUTES) {
          return false
        } else {
          return second < SECONDS
        }
      } else {
        return false
      }
    }
  }
}
const ariList = ref([])
const inspectionList = ref([])
// const roadList = ref([])
const sectionList = ref([])

const nameList = {
  channelName: null,
  airportName: null,
  inspectionLineName: null,
  legName: null
}

const handleChange = (type, val, ops) => {
  const key = `${type}Name`
  nameList[key] = ops?.name || null
  if (type === 'airport') {
    nameList['droneId'] = ops.droneId
  }
}

const legRangeIsLegName = (val, ops) => {
  nameList.legName = ops?.legRange || null
}

export const getNames = () => {
  return nameList
}

export const setNames = (data) => {
  for (var key in nameList) {
    nameList[key] = data[key] || null
  }
}

export const form = reactive({
  taskForm: {
    type: null,
    name: null,
    channelId: null,
    legId: null,
    legName: null,
    inspectionType: null,
    airportId: null,
    inspectionLine: null,
    isLive: null,
    startLongitude: null,
    startLatitude: null,
    endLongitude: null,
    endLatitude: null,
    inspectionTime: null,
    note: null,
    airportName: null,
    airportId: null,
    inspectionLineName: null,
    failReason: null,
    mileage: null,
    flyTime: null
  },
  taskRules: {
    type: [{ required: true, type: 'text', message: '请输入任务名称', trigger: 'blur' }],
    inspectionType: [{ required: true, type: 'text', message: '请选择巡检方式', trigger: 'blur' }],
    airportId: [{ required: true, type: 'text', message: '请选择巡检机场', trigger: 'blur' }],
    inspectionLineName: [{ required: true, type: 'text', message: '请选择巡检路线', trigger: 'blur' }],
    failReason: [{ required: true, type: 'text', message: '请输入失败原因', trigger: 'blur' }],
    flyTime: [{ required: true, type: 'text', message: '请输入飞行时长', trigger: 'blur' }],
    mileage: [{ required: true, type: 'text', message: '请输入飞行里程', trigger: 'blur' }],
    inspectionTime: [{ required: true, message: '请选择飞行时间(大于当前时间)', trigger: 'blur', validator(rule, value) {
      const oldTime = +new Date(value)
      const newTime = +new Date()
      return oldTime > newTime
    } }]
    // type: [{ required: true, type: 'number', message: '请选择任务类型', trigger: 'blur' }],
    // name: [{ required: true, message: '请输入', trigger: 'blur' }],
    // channelId: [{ required: true, message: '请选择公路', trigger: 'blur' }],
    // legId: [{ required: true, message: '请选择航段', trigger: 'blur' }],
    // isLive: [{ required: true, type: 'number', message: '请选择是否直播', trigger: 'blur' }],
    // startLongitude: [{ required: true, type: 'number', message: '请输入', trigger: ['blur', 'change', 'input'] }],
    // startLatitude: [{ required: true, type: 'number', message: '请输入', trigger: ['blur', 'change', 'input'] }],
    // endLongitude: [{ required: true, type: 'number', message: '请输入', trigger: ['blur', 'change', 'input'] }],
    // endLatitude: [{ required: true, type: 'number', message: '请输入', trigger: ['blur', 'change', 'input'] }],
  },
  formItem: [
    { type: 'input', span: 24, key: 'name', label: '任务名称', noEdit: ['preview', 'edit'], props: { maxlength: '30', placeholder: '' }},
    { type: 'select', span: 12, key: 'inspectionType', label: '巡检方式', noEdit: ['preview', 'update', 'edit'], props: { options: TASK_INSPECTS, placeholder: '', clearable: true }, func: {}},
    { type: 'input', span: 12, key: 'airportName', label: '巡检机场', noEdit: ['preview', 'edit'], props: { maxlength: '30', placeholder: '' }},
    { type: 'input', span: 12, key: 'inspectionLineName', label: '巡检路线', noEdit: ['preview', 'edit'], props: { valueField: 'id', maxlength: '30', placeholder: '' }},
    { type: 'datetime', span: 12, key: 'inspectionTime', label: '飞行时间', props: { type: 'datetime', inputReadonly: true, valueFormat: 'yyyy-MM-dd HH:mm:ss', format: 'yyyy-MM-dd HH:mm:ss', isDateDisabled: (ts) => { return ts < (Date.now() - 24 * 60 * 60 * 1000) }, isTimeDisabled: disableTime, actions: ['clear', 'confirm'], timePickerProps: { actions: ['confirm'] }}},
    { type: 'input', span: 24, key: 'note', label: '备注', noEdit: ['preview', 'edit'], props: { type: 'textarea', maxlength: '140', autosize: { minRows: 3, maxRows: 3 }}},
    { type: 't-step', span: 24, key: 'name', label: '任务状态' },
    { type: 'input', span: 24, key: 'failReason', label: '失败原因', noEdit: ['preview', 'edit'], props: { maxlength: '30', placeholder: '' }},
    { type: 'input', span: 12, key: 'flyTime', label: '飞行时长', noEdit: ['preview', 'edit'], props: { maxlength: '30', placeholder: '' }},
    { type: 'input', span: 12, key: 'mileage', label: '飞行里程', noEdit: ['preview', 'edit'], props: { maxlength: '30', placeholder: '' }}
    // { type: 'select', span: 12, key: 'type', label: '任务类型', noEdit: ['preview', 'update', 'edit'], props: { options: TASK_TYPE, placeholder: '请选择任务类型', clearable: true }, func: {}},
    // { type: 'input', span: 12, key: 'name', label: '任务名称', noEdit: ['preview', 'edit'], props: { maxlength: '30', placeholder: '请输入任务名称' }},
    // { type: 'select', span: 12, key: 'channelId', label: '航道', props: { options: roadList, labelField: 'code', valueField: 'id', placeholder: '请选择', clearable: true }, func: { 'update:value': handleChange.bind(null, 'channel') }},
    // { type: 'select', span: 12, key: 'legId', label: '航段', props: { options: sectionList, labelField: 'legRange', valueField: 'id', placeholder: '请选择', clearable: true }, func: { 'update:value': legRangeIsLegName }},
    // { type: 'select', span: 12, key: 'inspectionType', label: '巡检方式', noEdit: ['preview', 'update', 'edit'], props: { options: TASK_INSPECTS, placeholder: '请选择巡检方式', clearable: true }, func: {}},
    // { type: 'datetime', span: 12, key: 'inspectionTime', label: '飞行时间', props: { type: 'datetime', inputReadonly: true, valueFormat: 'yyyy-MM-dd HH:mm:ss', format: 'yyyy-MM-dd HH:mm:ss', isDateDisabled: (ts) => { return ts < (Date.now() - 24 * 60 * 60 * 1000) }, isTimeDisabled: disableTime, actions: ['clear', 'confirm'], timePickerProps: { actions: ['confirm'] }}},
    // { type: 'select', span: 12, key: 'airportId', label: '巡检机场', props: { options: ariList, labelField: 'name', valueField: 'id', placeholder: '请选择巡检机场', clearable: true }, mode: [2], func: { 'update:value': handleChange.bind(null, 'airport') }},
    // { type: 'select', span: 12, key: 'inspectionLine', label: '巡检路线', props: { options: inspectionList, labelField: 'name', valueField: 'id', placeholder: '请选择巡检路线', clearable: true }, mode: [2], func: { 'update:value': handleChange.bind(null, 'inspectionLine') }},
    // { type: 'pos-group', span: 24, prefix: 'start', label: '终点经纬度', prop: { showFeedback: false, required: true }, options: [
    //   { type: 'number', key: 'startLongitude', props: { maxlength: '5', placeholder: '终点经度' }},
    //   { type: 'number', key: 'startLatitude', props: { maxlength: '5', placeholder: '终点纬度' }}
    // ], mode: [3] },
    // { type: 'pos-group', span: 24, prefix: 'end', label: '终点经纬度', prop: { showFeedback: false, required: true }, options: [
    //   { type: 'number', key: 'endLongitude', props: { maxlength: '5', placeholder: '终点经度' }},
    //   { type: 'number', key: 'endLatitude', props: { maxlength: '5', placeholder: '终点纬度' }}
    // ], mode: [3] },
    // { type: 'radio', span: 12, key: 'isLive', label: '需要直播', options: LIVE_STREAM, mode: [3] },
    // { type: 'input', span: 24, key: 'note', label: '备注', props: { type: 'textarea', maxlength: '140', autosize: { minRows: 3, maxRows: 3 }}}
  ]
})

// 获取航道列表
// export const getRoadInfo = async function() {
//   const res = await getChannelListByUser()
//   if (res.code === 0) {
//     roadList.value = res.data
//   } else {
//     roadList.value = []
//   }
//   sectionList.value = []
// }

// 获取航段列表
export const getSectionInfo = async function(channelId, deptId) {
  if (channelId) {
    const params = { channelId, deptId }
    const res = await getIdDeptIdRoad(params)
    if (res.code === 0) {
      sectionList.value = res.data
    } else {
      sectionList.value = []
    }
  } else {
    sectionList.value = []
  }
}

// 巡检机场 列表
export const getAirInfo = async function() {
  const res = await getAirList()
  if (res.code === 0) {
    ariList.value = res.data.records
  } else {
    ariList.value = []
  }
}

// 巡检路线 列表
export const getWayInfo = async function(airportId) {
  if (airportId) {
    const res = await getAirLineByAir(airportId)
    if (res.code === 0) {
      inspectionList.value = res.data.map(it => ({
        ...it,
        name: it.name,
        value: it.id
      })) || []
    } else {
      inspectionList.value = []
    }
  } else {
    inspectionList.value = []
  }
}
